package com.wxtt.android.activity;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.fast.dev.frame.http.RequestParams;
import com.fast.dev.frame.ui.Bind;
import com.fast.dev.frame.utils.StringUtils;
import com.fast.dev.frame.utils.ToolUtils;
import com.wxtt.android.R;
import com.wxtt.android.common.CommonActivity;
import com.wxtt.android.recyclerview.StickyHeaderView;


/**
 * 说明：搜索
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2015/12/30 22:32
 * <p/>
 * 版本：verson 1.0
 */
public class SearchActivity extends CommonActivity{

    private EditText et_search;
    private ImageView iv_search;

    @Bind(id = R.id.tv)
    private TextView tv;
    @Bind(id = R.id.rv)
    private RecyclerView rv;

    @Override
    public int setRootView() {
        return R.layout.activity_search;
    }

    @Override
    protected boolean showToolbar() {
        return true;
    }

    @Override
    protected void onCustomToolbar(View view, Toolbar toolbar) {
        toolbar.setNavigationIcon(R.mipmap.ic_arrow_back_white_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        et_search = (EditText) view.findViewById(R.id.et_search);
        iv_search = (ImageView) view.findViewById(R.id.iv_search);
        iv_search.setOnClickListener(this);
        et_search.requestFocus();
        et_search.setOnKeyListener(onkey);
        ToolUtils.showSoftInput(et_search);
    }

    @Override
    public void onInit(Bundle bundle) {
        StickyHeaderView shv = new StickyHeaderView(rv,bundle);
    }

    @Override
    protected int setToolbarView() {
        return R.layout.menu_search;
    }

    @Override
    public void clickView(View view) {
        switch (view.getId()){
            case R.id.iv_search:
                checkValue(et_search.getText().toString());
                break;
        }
    }

    /**
     * 说明：检查搜索词
     * @param value
     */
    private void checkValue(String value){
        if (!StringUtils.isEmpty(value)){
            search(value);
            ToolUtils.hiddenSoftInput(et_search);
        }else {
            shortToast(R.string.search_value_empty);
        }
    }

    View.OnKeyListener onkey = new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER){
                checkValue(et_search.getText().toString());
                return true;
            }else {
                return false;
            }
        }
    };

    /**
     * 说明：搜索
     * @param value
     */
    private void search(String value){
        shortToast(value);
        String url = "http://api.17dianbo.com/index.php/person/newwcert";
        RequestParams params = new RequestParams();
//        params.put("api_token", "a57bcbe3d0104f9af926d6f7609a95ee");
//        params.put("rs", "1");
//        params.put("school", "123456");
//        params.put("profe", "+86");
//        params.put("atime", "18871584808");
//        params.put("isread", "18871584808");
//        params.put("country", "18871584808");
//        params.put("degreenum", "18871584808");
//        params.put("gcn", "18871584808");
//        params.put("img1", "18871584808");
//        params.put("img1", "18871584808");
//
//        HttpUtils.post(url, new StringHttpCallBack() {
//            @Override
//            public void onSuccess(String info) {
//                tv.setText(info);
//                shortToast(info);
//            }
//
//            @Override
//            public void onFailure(int errorCode, String msg) {
//                shortToast(msg);
//            }
//
//        });
//            @Override
//            public void onSuccess(String info) {
//                tv.setText(info);
//                shortToast(info);
//            }
//
//            @Override
//            public void onFailure(int errorCode, String msg) {
//                shortToast(msg);
//            }
//        });
    }

}
